package dao;

import pojo.User;
import pojo.Userinfo;
import pojo.Userregist;
import util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;

public class ImplementUser implements UserDao{
    //登录，判断用户名和密码是否正确
    @Override
    public boolean login(String usename, String password) {
        boolean flag=false;
        try {
            JDBC jdbc=new JDBC();
            jdbc.dbconn();
            ResultSet rs= JDBC.selectsql("select * from yonghu where usename='"+usename+"' and password='"+password+"'");
            while(rs.next()) {
                if (rs.getString("usename").equals(usename) && rs.getString("password").equals(password)) {
                    flag = true;
                    break;
                }
            }
            jdbc.closeconn();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 查找所有用户
     * @return
     */
    public ArrayList<Userinfo> showUsers(){
        ArrayList<Userinfo> listusers=new ArrayList<Userinfo>();
        JDBC jdbc=new JDBC();
        try {
            jdbc.dbconn();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ResultSet rs=jdbc.selectsql("select * from yonghu");
        while(true){
            try {
                if (!rs.next()) break;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            Userinfo userinfo=new Userinfo();
            try {
                userinfo.setId(rs.getString("id"));
                userinfo.setUsename(rs.getString("usename"));
                userinfo.setGender(rs.getString("gender"));
                userinfo.setTelephone(rs.getString("telephone"));
                userinfo.setAge(rs.getString("age"));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            listusers.add(userinfo);
        }
        jdbc.closeconn();
        return listusers;

    }

    @Override
//获得全部用户全部信息,封装好了全部用户
    public ArrayList<User> getUsers() {
        ArrayList<User> listuser=new ArrayList<User>();
        JDBC jdbc=new JDBC();
        try {
            jdbc.dbconn();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ResultSet rs=jdbc.selectsql("select * from yonghu");
        while(true){
            try {
                if (!rs.next()) break;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            User user=new User();
            try {
                user.setAddpower(Integer.parseInt(rs.getString("addpower")));
                user.setId(rs.getString("id"));
                user.setUsename(rs.getString("usename"));
                user.setPassword(rs.getString("password"));
                user.setGender(rs.getString("gender"));
                user.setTelephone(rs.getString("telephone"));
                user.setAge(rs.getString("age"));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            listuser.add(user);
        }
        jdbc.closeconn();
        return listuser;
    }

    /**
     * 获取所有用户信息与注册的用户名做比较
     * @return
     */
    public ArrayList<User> getUsersforregist() {
        ArrayList<User> listuser=new ArrayList<User>();
        JDBC jdbc=new JDBC();
        try {
            jdbc.dbconn();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ResultSet rs=jdbc.selectsql("select * from yonghu");
        while(true){
            try {
                if (!rs.next()) break;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            User user=new User();
            try {
                user.setId(rs.getString("id"));
                user.setUsename(rs.getString("usename"));
                user.setPassword(rs.getString("password"));
                user.setGender(rs.getString("gender"));
                user.setTelephone(rs.getString("telephone"));
                user.setAge(rs.getString("age"));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            listuser.add(user);
        }
        jdbc.closeconn();
        return listuser;
    }


    /**
     * 修改用户信息，封装成user对象再传进来，还有最好用id来判断修改的用户（需要改进）
     *
     * @param usename
     * @param password
     * @param gender
     * @param telephone
     * @return
     */
    @Override
    public boolean update(String usename, String password, String gender, String telephone,String age) {
        boolean flag=false;
        JDBC jdbc=new JDBC();
        try {
            jdbc.dbconn();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String sql="update yonghu set password='"+password+"',gender='"+gender+"',age='"+age+"',telephone='"+telephone+"'  where usename='"+usename+"'";
        int i=jdbc.addsql(sql);
        if(i>0)flag=true;
        else flag=false;
        jdbc.closeconn();
        return flag;
    }

    /**
     * 将注册成功了并封装好了的用户信息添加进数据库
     * @param userregist
     * @return
     */
    @Override
    public boolean addregist(Userregist userregist) {
        int id=0;
        boolean flag=false;
        JDBC jdbc2=new JDBC();
        try {
            jdbc2.dbconn();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ImplementUser implement = new ImplementUser();
        boolean flag2=implement.checkregist(userregist.getUsename());
        if(flag2==false){
            return flag;
        }
        JDBC jdbc=new JDBC();
        try {
            jdbc.dbconn();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String usename=userregist.getUsename();
        String password=userregist.getPassword();
        String telephone=userregist.getTelephone();
        String age=userregist.getAge();
        String gender=userregist.getGender();
        ArrayList<Integer> listid=new ArrayList<Integer>();
        ResultSet rs=jdbc.selectsql("select * from yonghu");
        while(true){
            try {
                if (!rs.next()) break;
                listid.add(Integer.valueOf(rs.getString("id")));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        id= listid.get(0);
        //遍历寻找id的最大值
        for(int i=0;i< listid.size();i++){
            if(listid.get(i)>id){
                id=listid.get(i);
            }
        }
        id++;
        String newid= String.valueOf(id);
        String sql="insert into yonghu values('"+newid+"','"+usename+"','"+password+"','"+age+"','"+telephone+"','"+gender+"','"+1+"')";
        int i=jdbc.addsql(sql);
        if(i>0)flag=true;
        else flag=false;
        jdbc.closeconn();
        return flag;
    }

    /**
     * 确认注册的用户名是否已经存在
     * @param usename
     * @return
     */
    @Override
    public boolean checkregist(String usename) {
        boolean flag = true;
        ImplementUser implement = new ImplementUser();
        //调用getUsers函数获得封装好了全部用户的集合
        ArrayList<User> listuser = implement.getUsersforregist();
        for (User user : listuser) {
            if (usename.equals(user.getUsename())) {
                flag=false;
                return flag;
            }
        }
        return flag;
    }

    @Override
    public Userregist listuser(String usename, String password, int age, String telephone, String gender) {
        return null;
    }

    /**
     * 获取请求的用户信息，封装成Userregist类以便添加进数据库
     * @param usename
     * @param password
     * @param age
     * @param telephone
     * @param gender
     * @return
     */
    @Override
    public Userregist listuser(String usename, String password, String age, String telephone, String gender) {
        Userregist listuser=new Userregist();
        listuser.setUsename(usename);
        listuser.setPassword(password);
        listuser.setAge(age);
        listuser.setTelephone(telephone);
        listuser.setGender(gender);
        return listuser;
    }

    /**
     * 根据用户名获取某个具体用户的详细信息--用户查询修改资料
     * @param usename
     * @return
     * @throws SQLException
     */
    public User getuser(String usename) throws SQLException {
        ArrayList<User> listusers=new ArrayList<>();
        ImplementUser implementUser=new ImplementUser();
        listusers=implementUser.getUsers();
        User user=new User();
        user.setUsename(usename);
        for (User listuser : listusers) {
            if(listuser.getUsename().equals(usename)){
                user.setAddpower(listuser.getAddpower());
                user.setAge(listuser.getAge());
                user.setTelephone(listuser.getTelephone());
                user.setPassword(listuser.getPassword());
                user.setGender(listuser.getGender());
                user.setId(listuser.getId());
                break;
            }
        }
        return user;
    }

    /**
     * 根据id查询用户的所有信息--用户修改信息
     * @param id
     * @return
     * @throws SQLException
     */
    public User finduser(String id) throws SQLException {
        ArrayList<User> listusers=new ArrayList<>();
        ImplementUser implementUser=new ImplementUser();
        listusers=implementUser.getUsers();
        User user=new User();
        for (User listuser : listusers) {
            if(listuser.getId().equals(id)){
                user.setId(listuser.getId());
                user.setAge(listuser.getAge());
                user.setTelephone(listuser.getTelephone());
                user.setPassword(listuser.getPassword());
                user.setGender(listuser.getGender());
                user.setUsename(listuser.getUsename());
                break;
            }
        }
        return user;
    }

    /**
     * 查找某区域的所有用户的信息
     * @param type
     * @return
     * @throws SQLException
     */
    public ArrayList<User> areaUsers(String type) throws SQLException {
        //元素不重复容器，提取某区域的用户
        HashSet<String> usenames=new HashSet<>();
        ArrayList<User> areausers = new ArrayList<>();
        ArrayList<User> listusers = new ArrayList<>();
        ImplementUser implementUser = new ImplementUser();
        listusers = implementUser.getUsers();
        JDBC jdbc = new JDBC();
        try {
            jdbc.dbconn();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ResultSet rs = jdbc.selectsql("select usename from biji where type='" + type + "'");
        while(rs.next()){
            usenames.add(rs.getString("usename"));
        }
        jdbc.closeconn();
        for (String usename : usenames) {
                    User user=new User();
                    user=implementUser.getareauser(usename);
                    areausers.add(user);
                }
        return areausers;
    }

    /**
     * 得到某个用户的具体信息--区域管理员
     * @param usename
     * @return
     * @throws SQLException
     */
    public User getareauser(String usename) throws SQLException {
        ArrayList<User> listusers=new ArrayList<>();
        ImplementUser implementUser=new ImplementUser();
        listusers=implementUser.getUsers();
        User user=new User();
        user.setUsename(usename);
        for (User listuser : listusers) {
            if(listuser.getUsename().equals(usename)){
                user.setAddpower(listuser.getAddpower());
                user.setAge(listuser.getAge());
                user.setTelephone(listuser.getTelephone());
                user.setGender(listuser.getGender());
                break;
            }
        }
        return user;
    }



    /**
     * 修改用户添加笔记的权限
     * @param user
     * @return
     */
    public boolean updatepower(User user){
        boolean flag=false;
        JDBC jdbc = new JDBC();
        try {
            jdbc.dbconn();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String sql=null;
        if(user.getAddpower()==1){
             sql="update yonghu set addpower='"+0+"'  where usename='"+user.getUsename()+"'";
        }else if(user.getAddpower()==0){
             sql="update yonghu set addpower='"+1+"'  where usename='"+user.getUsename()+"'";
        }
        int i=jdbc.addsql(sql);
        if(i>0)flag=true;
        else flag=false;
        jdbc.closeconn();
        return flag;
    }

    /**
     * 判断用户addpower的权限
     * @param usename
     * @return
     * @throws SQLException
     */
    public boolean findPower(String usename) throws SQLException {
        JDBC jdbc = new JDBC();
        boolean flag = false;
        try {
            jdbc.dbconn();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ResultSet rs = jdbc.selectsql("select * from yonghu");
        while (true) {
            try {
                if (!rs.next()) break;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if (rs.getString("usename").equals(usename)) {
User user=new User();
user.setUsename(rs.getString("usename"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setAge(rs.getString("age"));
user.setGender(rs.getString("gender"));
user.setAddpower(Integer.parseInt(rs.getString("addpower")));
if(user.getAddpower()==1){
    flag=true;
}
            }
        }
        return flag;
    }
}

